package com.justnow.offer;

/**
 * @author justnow
 * Created on 2020-09-02
 * Description
 */
public class Solution18 {
    public int candy(int[] ratings) {
        int sum = 0;
        int length = ratings.length;
        //如果一名学生评分比他左边的学生高，那么他应该比他的左边的学生得到更多糖果
        int[] left2right = new int[length];
        //如果一名学生评分比他右边的学生高，那么他应该比他右边的学生得到更多糖果
        int[] right2left = new int[length];

        for (int i = 0; i < length; i++) {
            left2right[i] = 1;
            right2left[i] = 1;
        }
        for (int i = 1; i < length; i++) {
            if (ratings[i] > ratings[i-1]) {
                left2right[i] = left2right[i-1] + 1;
            }
            
        }
        for (int j = length - 2; j >= 0 ; j--) {
            if (ratings[j] > ratings[j + 1]) {
                right2left[j] = right2left[j+1] + 1;
            }
        }

        for (int i = 0; i < length; i++) {
            sum += Math.max(left2right[i], right2left[i]);
        }
        return sum;
    }

    public static void main(String[] args) {
        Solution18 solution18 = new Solution18();
        solution18.candy(new int[]{1, 0, 2});
    }
}
